

import urllib.request

TARGET = 'http://www.baidu.com'


# 获取响应对象，调用读取方法(逐个字节读取)，设置UTF-8编码 读取返回HTML代码
response = urllib.request.urlopen(TARGET)
html = response.read().decode('UTF-8')
# print(html)

# 按指定字节个数返回内容
# response = urllib.request.urlopen(TARGET)
# print(response.read(64).decode('UTF-8'))

# 只读取一行
# response = urllib.request.urlopen(TARGET)
# print(response.readline().decode('UTF-8'))

# 逐行读取，封装成List，每个元素即为一行
# response = urllib.request.urlopen(TARGET)
# print(response.readlines())

# 获取响应状态码
print('status -> ', response.getcode())

# 获取响应头
print('respHeader -> ', response.getheaders())

